/*
 * @Date: 2023-04-04 09:39:46
 * @LastEditTime: 2023-05-04 14:49:29
 */
/* eslint-disable no-unused-vars */
import router from './'
import NProgress from 'nprogress'
import '@/components/NProgress/nprogress.less'
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
import storage from 'store'
import store from '../store/index'

const whiteList = ['login']
const defaultRoutePath = '/'

NProgress.configure({ showSpinner: false })

router.beforeEach((to, from, next) => {
  NProgress.start()
  to.meta && typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`)
  if (storage.get('token')) {
    if (to.path === '/user/login') {
      next({ path: defaultRoutePath })
      NProgress.done()
    } else next()
  } else {
    if (whiteList.includes(to.name)) next()
    else {
      next({ path: '/user/login' })
      NProgress.done()
    }
  }
})

router.afterEach((to) => {
  store.commit('SET_BREADCRUMB', to.matched)
  NProgress.done()
})
